/*
 * I2C.h
 *
 *  Created on: 21.11.2012
 *      Author: Michael
 */

#ifndef I2C_H_
#define I2C_H_

#include "stm32f4xx_i2c.h"
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"

struct I2C_cfg {
	uint32_t I2C_CLK; // RCC_APB1Periph_I2C1
	uint16_t I2C_SCL_PIN;
	GPIO_TypeDef* I2C_SCL_GPIO_PORT;
	uint32_t I2C_SCL_GPIO_CLK; //RCC_AHB1Periph_GPIOA
	uint8_t I2C_SCL_SOURCE;
	uint16_t I2C_SDA_PIN;
	GPIO_TypeDef* I2C_SDA_GPIO_PORT;
	uint32_t I2C_SDA_GPIO_CLK;
	uint8_t I2C_SDA_SOURCE;
	uint8_t I2C_AF;
	uint16_t I2C_SLAVE_ADDRESS7;
	uint32_t I2C_SPEED; /*!< Specifies the SCL frequency in Hz.
	 This parameter must be set to a value lower than 400kHz */
};

enum I2C_ID{
	I2C_ID1 = 1,
	I2C_ID2
};

class I2C {
public:
	I2C(int ID);
	virtual ~I2C();

	int write(uint8_t devAddr, uint8_t regAddr, uint8_t val);
	uint8_t read(uint8_t devAddr, uint8_t regAddr);
private:

	void init_I2C();

	int ID;
	I2C_TypeDef* I2Cx;
	I2C_cfg cfg;

};

#endif /* I2C_H_ */
